* Cohesive Institutions and Political Violence
* Thiemo Fetzer and Stephan Kyburz

* Prepares the Afrobarometer data
* Produces all tables that use Afrobarometer data


use "data files/afrobarometer_analysis_V4.dta", clear

merge m:1 ID_lgc month year using "data files/NGA.PAN.MERGED.L7.dta", keepusing(ID_2 lgc_elec_status)
keep if _merge==3
drop _merge 

merge m:1 ID_2 year month using "data files/LGC_ELEC_FILLED.dta"
keep if _merge==3
drop _merge 
replace lgc_elec_status  = naloclgcelec if lgc_elec_status==.

merge m:1 ID_lgc using "data files/FAAC_allocation_indices_2006.dta"
keep if _merge==3
drop _merge

merge m:1 year month using "data files/AB_OILPRICEl12month.dta"
drop _merge

merge m:1 ID_lgc year month using "data files/ABLGCALLOCATIONS.dta"
keep if _merge==3
drop _merge



*gen indexngabenchmark = totalindex * ngoilpricebenchmark
*label variable indexngabenchmark  "Index weight x Oil price"

gen indexoilpricel6month = totalindex * oilpricel6month

gen indexoilpricel12month = totalindex * oilpricel12month

gen indexoilpricel18month = totalindex * oilpricel18month

* Generate additional variables
foreach var in lgc_alloc_mol6m lgc_alloc_mol12m lgc_alloc_mol18m {
replace `var' = `var'/1000000000
}

label variable lgc_alloc_mol6m "LGC Allocations 6 months prior to survey"
label variable lgc_alloc_mol12m "LGC Allocations 12 months prior to survey"
label variable lgc_alloc_mol18m "LGC Allocations 18 months prior to survey"

label variable oilpricel6month "Oil Price [avg of last 6 months]"
label variable oilpricel12month "Oil Price [avg of last 12 months]"
label variable oilpricel18month "Oil Price [avg of last 18 months]"


label variable indexoilpricel6month "Index Weight x Oil Price [avg of last 6 months]"
label variable indexoilpricel12month "Index Weight x Oil Price [avg of last 12 months]"
label variable indexoilpricel18month "Index Weight x  Oil Price [avg of last 18 months]"

label variable lgc_elec_status "Elected"


rename AB_ethngr_treated_unfair_tails  AB_ethngr_treated_unf_tails
***THIS IS TURNING VARIABLES THAT HAVE MULTIPLE CATEGORIES INTO DUMMIES
foreach var in AB_approve_MP_assembly AB_approve_governor AB_approve_lgc_councilor AB_army_rule AB_arrested AB_attend_comm_meet AB_attend_demonstration AB_brotherhood   AB_contact_MP AB_contact_gov_official AB_contact_pol_party_off AB_contact_religious_leader AB_contact_traditional_ruler AB_corrupt_govern_officials AB_corrupt_governors AB_corrupt_judges AB_corrupt_lg_officials AB_corrupt_lgc_chairmen AB_corrupt_lgc_councilor AB_corrupt_national_assembly AB_corrupt_national_officials AB_corrupt_police AB_corrupt_president AB_corrupt_state_assembly AB_corrupt_tax_officials AB_corrupt_traditional_lead AB_country_right_dir AB_discussed_politics AB_econ_cond_country AB_econ_cond_in_12M AB_econ_cond_vs_12Mago  AB_election_rally AB_ethngr_treated_unfair AB_family_nocash AB_family_nocleanwater AB_family_nocookfuel AB_family_nofood AB_family_nomedicin AB_family_schoolexpens AB_fear_political_violence AB_female AB_gov_accountability AB_gov_help_police AB_gov_medical_services AB_gov_school_placement AB_government_salary AB_household_size AB_identity AB_identity_treated_unfair AB_importance_elections AB_income AB_influence_tradit_lead AB_lgc_community_clean AB_lgc_consult AB_lgc_council_listen AB_lgc_creation AB_lgc_gov_services AB_lgc_healthstandards AB_lgc_land_management AB_lgc_license_fee AB_lgc_market_maintenance AB_lgc_responsibility AB_lgc_road_maintenance AB_liv_cond_compared AB_male AB_opposition_silenced AB_party_competition_viol AB_party_division AB_paying_job AB_pers_liv_cond AB_pers_liv_cond_in_12M AB_pers_liv_cond_vs_12Mago AB_physically_attacked    AB_psu_cellphone_service AB_psu_commroadblock AB_psu_electricity AB_psu_healthclinic AB_psu_marketstalls AB_psu_pavedroad AB_psu_police AB_psu_policemen AB_psu_polroadblock AB_psu_postoffice AB_psu_privroadblock AB_psu_school AB_psu_sewage AB_psu_soldiers AB_psu_water AB_raise_issue AB_refuse_pay_tax   AB_role_news_media AB_role_opposition_party AB_rural AB_satisfaction_democracy AB_state_gov_responsibility AB_support_democracy AB_threatened_interview AB_trust_INEC AB_trust_army AB_trust_courts AB_trust_election_tribunals AB_trust_lgc AB_trust_national_assembly AB_trust_opposition_party AB_trust_other_ethnicity AB_trust_own_ethnicity AB_trust_police AB_trust_president AB_trust_ruling_party AB_trust_state_governor AB_trust_traditional_leaders AB_type_shelter AB_unsafe_home AB_unsafe_walking AB_urban AB_use_violence_for_cause AB_violence_justified AB_vote_buying_2003 AB_vote_buying_2011 AB_weight_factor AB_work_for_candidate AB_education AB_ethngr_treated_unf_tails   AB_language_equal_ethnicity AB_excluded_ethn AB_excluded_ethnfam  {

di "`var' \n"
local varstem = substr("`var'",1,22)
qui tab `var' if `var'<6, gen(`varstem'_)

forvalues i=1(1)`r(r)' {
local lab: variable label `var' 

local tlab: variable label `varstem'_`i' 
local start = strlen("`var'")+3
local ttlab =substr("`tlab'",`start',.)
local tlab =  subinstr("`ttlab'"," ","",.)
local tlab =  lower("`tlab'")
local tlab =  subinstr("`tlab'","/","",.)
local tlab =  subinstr("`tlab'","(","",.)
local tlab =  subinstr("`tlab'",")","",.)
local tlab =  subinstr("`tlab'",".","",.)
local tlab =  subinstr("`tlab'",",","",.)
local tlab =  subinstr("`tlab'","-","",.)
local tlab =  subinstr("`tlab'","'","",.)
local tlab =  substr("`tlab'",1,25)

label variable `varstem'_`i' "`ttlab' `lab'"

}
}



gen AB_use_violence = AB_use_violence_for_ca_4+AB_use_violence_for_ca_5 

gen AB_fear_polvio = 1-AB_fear_political_viol_4

gen AB_phys_attack = AB_physically_attacked>=3 if AB_physically_attacked!=.



**

global controls="AB_paying_job AB_age  AB_male  AB_urban AB_education"
global victim ="AB_fear_polvio AB_phys_attack AB_use_violence"


egen idelec = group(lgc_elec_status ID_lgc )
egen syrmo = group(ID_sn yrmo)
 
global econ ="AB_econ_cond_country AB_econ_cond_vs_12Mago AB_econ_cond_in_12M"

global livingcond ="AB_pers_liv_cond AB_pers_liv_cond_vs_12Mago AB_pers_liv_cond_in_12M"

global institutions ="AB_support_democracy AB_trust_lgc AB_approve_lgc_councilor"

global responsiveness = " AB_lgc_consult AB_lgc_council_listen"


global victim ="AB_fear_polvio AB_phys_attack AB_use_violence"
global grievances ="AB_approve_lgc_councilor AB_corrupt_lgc_councilor AB_trust_lgc"
global grievancesnational ="AB_approve_MP_assembly AB_corrupt_national_assembly AB_trust_national_assembly"
global grievancesstate ="AB_approve_governor AB_corrupt_state_assembly AB_trust_state_governor"

gen const = 1

**VALIDATING THE STATE LEVEL MEASURE

********************************************************************************
****  TABLE A18: Validation of measure of exclusion of ethnic groups

gen SometimesUnfairly = AB_ethngr_treated_unfa_2+AB_ethngr_treated_unfa_3+AB_ethngr_treated_unfa_4
global ethnicgrievances ="AB_ethngr_treated_unfair SometimesUnfairly AB_ethngr_treated_unf_tails"


foreach list in ethnicgrievances  {

local k=1
foreach var in ${`list'}  {


foreach type in "AB_excluded_ethnfam" "AB_excluded_ethn"  {

foreach fe in  "ID_sn" "ID_sn yrmo_survey" "ID_lgc" "ID_lgc yrmo_survey "  {

foreach cc in ""  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var'  `cc' `type'  , absorb(`fe') vce(cl ID_lgc) old
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}
}

local tt = subinstr("`type'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)

esttab using "tables/fragment-ab-`var'-`list'-`type'-ols.tex" , replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

estimates clear
}
local k= `k'+1

}
}


foreach list in ethnicgrievances {

local k=1
foreach var in ${`list'}  {


foreach type in "AB_excluded_ethnfam c.lgc_elec_status#c.AB_excluded_ethnfam" "AB_excluded_ethn c.lgc_elec_status#c.AB_excluded_ethn"  {

foreach fe in  "ID_sn yrmo_survey " "ID_lgc yrmo_survey "  {

foreach cc in "" "$controls" {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var'  `cc' `type'  lgc_elec_status, absorb(`fe') vce(cl ID_lgc)  old
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}
}
}
local tt = subinstr("`type'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)

*
esttab using "tables/fragment-ab-`var'-int-`list'-ols.tex" , replace nolines nomtitles  fragment nowrap drop(lgc_elec_status $controls)    label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

estimates clear

local k= `k'+1

}
}
********************************************************************************




********************************************************************************
****  TABLE 6: Individual Level Perception of Local Governance
****  TABLE 3: Resource rents, elected (vs appointed) local governments and individual level victimization and participation in conflict 
****  TABLE A19: LGC Allocations and Grievances towards National Political Institutions
****  TABLE A20: LGC Allocations and Grievances towards State level Political institutions


foreach list in victim grievances grievancesnational grievancesstate {

foreach mm in "6" "12" "18" {
foreach fe in yrmo_survey  {

local k=1
foreach type in "lgc_alloc_mol`mm'm c.lgc_elec_status#c.lgc_alloc_mol`mm'm" {
foreach var in ${`list'}  {
 
foreach cc in "" "$controls" {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' lgc_elec_status  `type'  `cc', absorb(ID_lgc  `fe') vce(cl ID_lgc)  old
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}
}
local tt = subinstr("`type'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
 

esttab using "tables/fragment-ab`mm'-`list'-ols-`fe'.tex" , replace nolines nomtitles  fragment nowrap drop(lgc_elec_status $controls)  label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

estimates clear

local k= `k'+1
}
}
}
}


 

foreach mm in "6" "12" "18" {
foreach fe in yrmo_survey  {
foreach list in victim  grievances grievancesnational {

local k=1
foreach type in "lgc_alloc_mol`mm'm c.lgc_elec_status#c.lgc_alloc_mol`mm'm" "indexoilpricel`mm'month  c.lgc_elec_status#c.indexoilpricel`mm'month" "(lgc_alloc_mol`mm'm c.lgc_elec_status#c.lgc_alloc_mol`mm'm = indexoilpricel`mm'month  c.lgc_elec_status#c.indexoilpricel`mm'month  )" {
foreach var in ${`list'}  {
 
foreach cc in "" "$controls" {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' lgc_elec_status `cc' `type'  , absorb(ID_lgc `fe') vce(cl ID_lgc)  old
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}
}
local tt = subinstr("`type'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)

if("`k'"=="3") {
esttab using "tables/fragment-ab`mm'-`list'-iv-`fe'.tex" , replace nolines nomtitles  fragment nowrap drop(lgc_elec_status $controls)  label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(wid N LGC ymean , labels("Kleibergen-Paap weak IV" "Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
} 
else if("`k'"=="2") {
esttab using "tables/fragment-ab`mm'-`list'-rf-`fe'.tex" , replace nolines nomtitles  fragment nowrap  drop(lgc_elec_status $controls) label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}
else if("`k'"=="1") {
esttab using "tables/fragment-ab`mm'-`list'-ols-`fe'.tex" , replace nolines nomtitles  fragment nowrap drop(lgc_elec_status $controls)  label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}

estimates clear

local k= `k'+1
}
}
}
}

